#!/usr/bin/env python
# $Id: profiledialog.py 70 2009-11-20 15:45:15Z michon1992 $
#
# This file is part of Phennui.
#
# Phennui is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Phennui is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Phennui.  If not, see <http://www.gnu.org/licenses/>.

import pygtk
pygtk.require('2.0')
import gtk, gobject
import re
import fileeditor

class profiledialog:
    
    def __init__(self, ui):
        self.ui = ui
 
    def run(self, profile):
        self.profile = profile
        
        mapping = {'nickname': 'nick', 'realname': 'name', 'password': 'password', 'server': 'host', 'port': 'port', 'owner': 'owner'}
        for entryname, configname in mapping.iteritems():
            self.ui.get_object('profiledialog_%s_entry' % entryname).set_text(str(getattr(self.profile.data, configname, '')))
        self.ui.get_object('profiledialog_prefix_entry').set_text(re.sub(r'\\(.)', r'\1', self.profile.data.prefix))
        self.ui.get_object('profiledialog_admins_buffer').set_text('\n'.join(self.profile.data.admins))
        self.ui.get_object('profiledialog_channels_buffer').set_text('\n'.join(self.profile.data.channels))
        
        self.ui.get_object('profiledialog').run()
        
    def event_response(self, dialog, response):
        if response == gtk.RESPONSE_OK:
            if True:
                mapping = {'nickname': 'nick', 'realname': 'name', 'password': 'password', 'server': 'host', 'owner': 'owner'}
                for entryname, configname in mapping.iteritems():
                    setattr(self.profile.data, configname, self.ui.get_object('profiledialog_%s_entry' % entryname).get_text())
                self.profile.data.prefix = re.escape(self.ui.get_object('profiledialog_prefix_entry').get_text())
                try:
                    self.profile.data.port = int(self.ui.get_object('profiledialog_port_entry').get_text())
                except:
                    self.profile.data.port = 6667
                self.profile.data.admins = self.ui.get_object('profiledialog_admins_buffer').get_text(self.ui.get_object('profiledialog_admins_buffer').get_start_iter(), self.ui.get_object('profiledialog_admins_buffer').get_end_iter()).split('\n')
                if self.profile.data.owner not in self.profile.data.admins:
                    self.profile.data.admins.insert(0, self.profile.data.owner);
                self.profile.data.channels = self.ui.get_object('profiledialog_channels_buffer').get_text(self.ui.get_object('profiledialog_channels_buffer').get_start_iter(), self.ui.get_object('profiledialog_channels_buffer').get_end_iter()).split('\n')
                    
                f = open(self.profile.fname, 'w')
                f.write(str(self.profile.data))
                f.close()
        dialog.hide()

